import axios from 'axios';
import store from '../store';
// 创建axios实例
const service = axios.create({
  baseURL: process.env.VUE_APP_URL,
  //   baseURL: 'http://132.232.87.95:4000',
  baseURL: 'http://localhost:4000',
  timeout: 50000 // 请求超时时间(因为需要调试后台,所以设置得比较大)
})

// request拦截器
service.interceptors.request.use(
  config => {
    // 给请求头添加sessionToken
    if (store.state.token) {
      config.headers['sessionToken'] = store.state.token;
    }
    return config
  },
  error => {
    console.log(error) // for debug
    Promise.reject(error)
  }
)

// response 拦截器
service.interceptors.response.use(
  response => {
    /**
     * code为非20000是抛错 可结合自己业务进行修改
     */
    const res = response.data
    if (res.code == '666') {
      return response.data
    } else {
      alert(res.data.msg);
    }
  },
  error => {
    alert('网络异常');
  }
)
export default service
